package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.MaintenanceChangeDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Maintenance;
import com.neusoft.neuiotms.dcam.fixed.model.MaintenanceChange;
import com.neusoft.neuiotms.dcam.fixed.model.enums.MaintenanceChangeStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.MaintenanceChangeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.MaintenanceRepository;

@Component
public class MaintenanceChangeConverter extends AbstractConverter<MaintenanceChange, MaintenanceChangeDTO> {

    @Autowired
    private MaintenanceChangeRepository changeRepository;

    @Autowired
    private MaintenanceRepository maintenanceRepository;

    @Autowired
    private AssetConverter assetConverter;

    @Override
    public MaintenanceChange toModel(final MaintenanceChangeDTO dto) {
        MaintenanceChange model;
        if (dto.isNew()) {
            model = new MaintenanceChange();
        } else {
            model = this.changeRepository.getOne(dto.getId());
        }
        model.setCode(dto.getCode());
        model.setUpdateTime(new Date());
        model.setChangeStatus(StringUtils.isEmpty(dto.getChangeStatus()) ? MaintenanceChangeStatus.created
                : MaintenanceChangeStatus.valueOf(dto.getChangeStatus()));

        boolean mainteanceFlag = false;
        if (((null == dto.getMaintenanceId()) && (dto.getMaintenanceEnd() != null))
                || (dto.getMaintenanceStart() != null)
                || !StringUtils.isEmpty(dto.getMaintenanceUserId()) || !StringUtils
                        .isEmpty(dto.getMaintenanceNote())
                || !StringUtils.isEmpty(dto.getMaintenanceUserName())) {
            mainteanceFlag = true;
        }
        final Maintenance maintenanceModel = null != dto.getMaintenanceId()
                ? this.maintenanceRepository.getOne(dto.getMaintenanceId()) : mainteanceFlag ? new Maintenance() : null;
        if (maintenanceModel != null) {
            maintenanceModel.setMaintenanceEnd(dto.getMaintenanceEnd());
            maintenanceModel.setMaintenanceStart(dto.getMaintenanceStart());
            maintenanceModel.setMaintenanceUserId(dto.getMaintenanceUserId());
            maintenanceModel.setMaintenanceUserName(dto.getMaintenanceUserName());
            maintenanceModel.setMaintenanceNote(dto.getMaintenanceNote());
            model.setMaintenance(maintenanceModel);
        }

        return model;
    }

    @Override
    public MaintenanceChangeDTO toDTO(final MaintenanceChange model, final boolean forListView) {
        final MaintenanceChangeDTO dto = new MaintenanceChangeDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setChangeStatus(model.getChangeStatus().getCode());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        if (model.getManufacturer() != null) {//厂商
            dto.setManufacturerId(model.getManufacturer().getId());
            dto.setManufacturerName(model.getManufacturer().getName());
            dto.setManufacturerPerson(model.getManufacturer().getPerson());
            dto.setManufacturerPhone(model.getManufacturer().getPhone());
        }

        if (model.getMaintenance() != null) {//维保
            dto.setMaintenanceId(model.getMaintenance().getId());
            dto.setMaintenanceEnd(model.getMaintenance().getMaintenanceEnd());
            dto.setMaintenanceStart(model.getMaintenance().getMaintenanceStart());
            dto.setMaintenanceUserId(model.getMaintenance().getMaintenanceUserId());
            dto.setMaintenanceUserName(model.getMaintenance().getMaintenanceUserName());
            dto.setMaintenanceNote(model.getMaintenance().getMaintenanceNote());
        }

        if ((model.getAssets() != null) && !model.getAssets().isEmpty()) {
            final List<AssetDTO> list = new ArrayList<>();
            model.getAssets().forEach(assetModel -> {
                list.add(this.assetConverter.toDTO(assetModel));
            });
            dto.setAssets(list);
        }

        return dto;
    }
}
